﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using HotelManagement.DTO;

namespace HotelManagement.DAO
{
    class RoomDAO:DataProvider
    {
        public RoomDAO()
            :base()
        {

        }
        public bool _Insert(RoomDTO room)
        {
            SqlCommand cmd = new SqlCommand("Insert_A_Room", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add("@RoomName", SqlDbType.NChar).Value = room.RoomName;
                cmd.Parameters.Add("@RoomTypeID", SqlDbType.Int).Value = room.RoomTypeID;
                cmd.Parameters.Add("@Direction", SqlDbType.NVarChar).Value = room.Direction;
                cmd.Parameters.Add("@Status", SqlDbType.Int).Value = room.Status;
                if (connection.State == System.Data.ConnectionState.Closed)
                {
                    if (!Connect())
                    {
                        return false;
                    }
                }
                if (cmd.ExecuteNonQuery()<=0)
                {
                    return false;
                }
            }
            catch (System.Exception e)
            {
                return false;
            }
            return true;
        }

        public bool Insert(RoomDTO room)
        {
            string strInsert = "insert into room(RoomName,RoomTypeID,NumOfBed,NumOfPerson,Direction,Views,Status,LastOwner) values (";
            strInsert += "N'" + room.RoomName + "',";
            strInsert += room.RoomTypeID + ",";
            strInsert += room.NumOfBed + ",";
            strInsert += room.NumOfPerson + ",";
            strInsert += "N'" + room.Direction + "',";
            strInsert += "N'" + room.Views + "',";
            strInsert += room.Status + ",";
            strInsert += room.LastOwner + ")";
            return base._ExecuteQuery(strInsert);
        }
        //Hàm này chỉ để cập nhật lại thông tin cứng của phòng, không cập nhật giá trị của Status và LastOwner
        //Note: the UpdateInfo function does not update the Status value of the room
        //the Room's property Status is updated in Rent and Bill business logic
        public bool UpdateInfo(RoomDTO room)
        {
            string strUpdate = "update room set ";
            strUpdate += "room.RoomName = N'" + room.RoomName + "',";
            strUpdate += "room.RoomTypeID = " + room.RoomTypeID + ",";
            strUpdate += "room.NumOfBed = " + room.NumOfBed + ",";
            strUpdate += "room.NumOfPerson = " + room.NumOfPerson + ",";
            strUpdate += "room.Direction = N'" + room.Direction + "',";
            strUpdate += "room.Views = N'" + room.Views + "' where room.RoomID = " + room.RoomID;
 
            return base._ExecuteQuery(strUpdate);
        }
        //Hàm này dùng để cập nhật lại hai thông tin là Status và LastOwner cho phòng
        //Hàm được thực hiện khi thực hiện thuê phòng
        public bool UpdateWhenRent(RoomDTO room)
        {
            string strUpdate = "update room set ";
            strUpdate += "room.Status =" + room.Status + ",";
            strUpdate += "room.LastOwner=" + room.LastOwner;
            strUpdate += " where room.RoomID = " + room.RoomID;

            return base._ExecuteQuery(strUpdate);
        }
        public DataTable SelectAllWithTypeName()
        {
            string strSelect = "select room.RoomID,room.RoomName,roomtype.RoomTypeName,room.NumOfBed,room.NumOfPerson,room.Direction,room.Views,room.Status,room.LastOwner ";
            strSelect += "from room inner join roomtype ";
            strSelect += "on room.RoomTypeID= roomtype.RoomTypeID";

            return base._Select(strSelect);
        }
   
        public int SelectLastInsertID()
        {
            return base.SelectLastInsertID("room", "RoomID");
        }
        public DataTable Select()
        {
            string strSelect = "select * from room";
            return base._Select(strSelect);
        }
        public RoomDTO SelectRoomFromID(int pID)
        {
            if (pID==0)
            {
                return null;
            }
            DataTable tbl = this.Select();
            foreach (DataRow row in tbl.Rows)
            {
                if (int.Parse(row["RoomID"].ToString())==pID)
                {
                    RoomDTO room = new RoomDTO();
                    room.RoomID = pID;
                    room.RoomName = row["RoomName"].ToString();
                    room.RoomTypeID = int.Parse(row["RoomTypeID"].ToString());
                    room.NumOfBed = int.Parse(row["NumOfBed"].ToString());
                    room.NumOfPerson = int.Parse(row["NumOfPerson"].ToString());
                    room.Direction = row["Direction"].ToString();
                    room.Views = row["Views"].ToString();
                    room.Status = int.Parse(row["Status"].ToString());
                    room.LastOwner = int.Parse(row["LastOwner"].ToString());
                    return room;
                }
            }
            return null;
        }
    }
}
